package com.se2.antiqueexhibition.Controller;

import com.se2.antiqueexhibition.Service.CollectionService;
import com.se2.antiqueexhibition.pojo.CulturalRelic;
import com.se2.antiqueexhibition.pojo.User;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping
public class CollectionController {
    @Resource
    private CollectionService collectionService;

    @RequestMapping("/addcollection/{id}")
    public String addCollection(HttpSession session, @PathVariable int id) {
        if (session.getAttribute("user") == null) {
            return "redirect:/user/personal";
        }
        User user = (User) session.getAttribute("user");
        collectionService.addCollection(user.getId(), id);
        List<Integer> collection = collectionService.getCollectionIdsByUserId(user.getId());
        session.setAttribute("collection",collection);
        return "redirect:/antiquelist";
    }

    @RequestMapping("/collection")
    public String collection(HttpSession session, ModelMap map) {
        if (session.getAttribute("user") == null) {
            return "redirect:/user/personal";
        }
        User user = (User) session.getAttribute("user");
        List<CulturalRelic> culturalRelicList = collectionService.showCollection(user.getId());
        System.out.println(culturalRelicList);
        map.addAttribute("culturalRelicList", culturalRelicList);
        return "collection";
    }

    @RequestMapping("/cancelcollection/{id}")
    public String cancelCollection(HttpSession session, @PathVariable int id) {
        if (session.getAttribute("user") == null) {
            return "redirect:/user/personal";
        }
        User user = (User) session.getAttribute("user");
        collectionService.cancelCollection(user.getId(), id);
        List<Integer> collection = collectionService.getCollectionIdsByUserId(user.getId());
        session.setAttribute("collection",collection);
        System.out.println(collection);
        return "redirect:/collection";
    }

}
